import { defineStore } from 'pinia'
import getItems from '../api/items'
import getCurrent from '../api/current'

export const useItemsStore = defineStore('items', {
  state: () => ({
    currentCategory: {},
    categoryList: [],
    currentSubCategory: []
  }),
  actions: {
    async fetchGetItems() {
      try {
        let res = await getItems()
        if (Object.keys(res).length > 0) {
          let { data } = res
          this.currentCategory = data.currentCategory
          this.categoryList = data.categoryList
          this.currentSubCategory = data.currentSubCategory
        }
      } catch (error) {
        console.error('获取分类数据失败：', error)
      }
    },
    async change(id) {
      try {
        let res = await getCurrent(id)

        let { data } = res
        this.currentCategory = data.currentCategory
        this.currentSubCategory = data.currentSubCategory
      } catch (error) {
        console.error('获取分类数据失败：', error)
      }
    }
  }
})
